package com.krmes.mes.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

import com.krmes.mes.R;
import com.krmes.mes.util.TDevice;

public class WaitDialog extends Dialog {
    private TextView _messageTv;

    public WaitDialog(Context context) {
        super(context);
        init(context);
    }

    public WaitDialog(Context context, int defStyle) {
        super(context, defStyle);
        init(context);
    }

    protected WaitDialog(Context context, boolean cancelable, OnCancelListener listener) {
        super(context, cancelable, listener);
        init(context);
    }

    public static boolean dismiss(WaitDialog dialog) {
        if (dialog == null) {
            return true;
        }
        dialog.dismiss();
        return false;
    }

    public void onBackPressed() {
        super.onBackPressed();
        dismiss();
    }

    public static void hide(Context context) {
        if (context instanceof DialogControl) {
            ((DialogControl) context).hideWaitDialog();
        }
    }

    public static boolean hide(WaitDialog dialog) {
        if (dialog == null) {
            return true;
        }
        dialog.hide();
        return false;
    }

    private void init(Context context) {
        setCancelable(false);
        requestWindowFeature(1);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_wait, null);
        this._messageTv = (TextView) view.findViewById(R.id.waiting_tv);
        setContentView(view);
    }

    public static void show(Context context) {
        if (context instanceof DialogControl) {
            ((DialogControl) context).showWaitDialog();
        }
    }

    public static boolean show(WaitDialog waitdialog) {
        if (waitdialog == null) {
            return true;
        }
        waitdialog.show();
        return false;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (TDevice.isTablet()) {
            int i = (int) TDevice.dpToPixel(360.0f);
            if (((float) i) < TDevice.getScreenWidth()) {
                LayoutParams params = getWindow().getAttributes();
                params.width = i;
                getWindow().setAttributes(params);
            }
        }
    }

    public void setMessage(int message) {
        this._messageTv.setText(message);
    }

    public void setMessage(String message) {
        this._messageTv.setText(message);
    }

    public void hideMessage() {
        this._messageTv.setVisibility(View.INVISIBLE);
    }
}
